package cn.com.dycc.sys_manage.dictionarysysmanagement.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dycc.common.controller.BaseController;
import cn.com.dycc.sys_manage.dictionarysysmanagement.entity.Dictionary;
import cn.com.dycc.sys_manage.dictionarysysmanagement.entity.DictionaryResult;
import cn.com.dycc.sys_manage.dictionarysysmanagement.param.DicParam;
import cn.com.dycc.sys_manage.dictionarysysmanagement.service.DictNotFoundException;
import cn.com.dycc.sys_manage.dictionarysysmanagement.service.DictionaryService;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.JsonResult;
/**
 * 
 * Description: 字典表controller
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2017年8月16日 下午4:16:09.
 * Update Date Time: 
 * @see
 */
@RequestMapping("/dictionary")
@Controller
public class DictionaryController extends BaseController{
	@Resource
	private DictionaryService dictionaryService;
	
	//增加字典表数据
	@RequestMapping("/add.do")
	@ResponseBody
	public JsonObjectResult addDictionary(HttpServletRequest request,Dictionary dict){
//		System.out.println(dict);
		String opt_user_id = request.getSession().getAttribute("id").toString();
		String org_id = request.getSession().getAttribute("org_id").toString();
//		System.out.println(org_id);
		dict.setOpt_user_id(opt_user_id);
		dict.setOrg_id(org_id);
		String b = dictionaryService.addDictionary(dict);
		return new JsonObjectResult(b);
	}
	
	//更新字典表数据
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonObjectResult udpateDictionary(HttpServletRequest request,Dictionary dict){
//		System.out.println(dict);
		String update_user_id = request.getSession().getAttribute("id").toString();
		String org_id = request.getSession().getAttribute("org_id").toString();
		dict.setOpt_user_id(update_user_id);
		dict.setOrg_id(org_id);
		String  b = dictionaryService.updateDictionary(dict);
		return new JsonObjectResult(b);
	}
	
	//批量删除字典表
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonObjectResult deleteDictionary(HttpServletRequest req,String []ids){
		String b = dictionaryService.deleteDictionary(req,ids);
		return new JsonObjectResult(b);
	}
	
	//查询字典表所有信息 ;模糊查询
	@RequestMapping("/findList.do")
	@ResponseBody
	public JsonObjectResult listDictionary(HttpServletRequest request,DicParam dicParam){
		String org_id = request.getSession().getAttribute("org_id").toString();
		String count = dictionaryService.countDict(org_id, dicParam);
		List<DictionaryResult> list = 
				dictionaryService.findAllDict(org_id, dicParam);
		return new JsonObjectResult(count,list);
	}
	
	//使用spring管理异常
	@ExceptionHandler(DictNotFoundException.class)
	@ResponseBody
	public JsonObjectResult dictExceptoin(DictNotFoundException e) {
		e.printStackTrace();
		return new JsonObjectResult(11,e);
	}
	
	//用于查询精确查询字典表集合
	@RequestMapping("/list.do")
	@ResponseBody
	public JsonObjectResult dicList(HttpServletRequest request,DicParam dicParam){
		String org_id = request.getSession().getAttribute("org_id").toString();
		String count = dictionaryService.countDict(org_id, dicParam);
		List<DictionaryResult> list = 
				dictionaryService.dicList(org_id, dicParam);
		return new JsonObjectResult(count,list);
	}
	
	//查询字典表数据keyandval
	@RequestMapping("/findic.do")
	@ResponseBody
	public JsonObjectResult findKeyAndVal(HttpServletRequest request,String tab_name,String column_name){
		List<Map<String,Object>> list= dictionaryService.findKeyAndVal(request, tab_name, column_name);
		return new JsonObjectResult(list);
	}
	
	
	@RequestMapping("/findone.do")
	@ResponseBody
	public JsonObjectResult findOne(String id,HttpServletRequest request){
		Dictionary dict = dictionaryService.findDictionary(id, request);
		return new JsonObjectResult(dict);
	}
	
}
